const models = require('../db')
const express = require('express')
const router = express.Router()
const mysql = require('mysql')
const $sql = require('../sqlMap')
// 连接数据库
const conn = mysql.createConnection(models.mysql)
conn.connect()
const jsonWrite = function(res, ret) {
  if (typeof ret === 'undefined') {
    res.json({
      code: '1',
      msg: '操作失败'
    })
  } else {
    res.json(ret)
  }
}
// 接口：增加信息
router.post('/addStu', (req, res) => {
  const sql = $sql.Stu.add
  const params = req.body
  console.log('添加', params)
  conn.query(
    sql,
    [
      params.mailbox,
      params.name,
      params.role_mg,
      params.state_mg,
      params.phone,
      params.password
    ],
    function(err, result) {
      if (err) {
        console.log(err)
      }
      if (result) {
        jsonWrite(res, result)
      }
    }
  )
})

// 接口：增加信息
router.post('/addequipment', (req, res) => {
  const sql = $sql.Stu.addequipment
  const params = req.body
  console.log('添加', params)
  conn.query(
    sql,
    [
      params.sensor,
      params.number,
      params.ID,
      params.data,
      params.company,
      params.username
    ],
    function(err, result) {
      if (err) {
        console.log(err)
      }
      if (result) {
        jsonWrite(res, result)
      }
    }
  )
})

// 接口：查询全部
router.get('/showequipmentStu', (req, res) => {
  const sql = $sql.Stu.showequipment
  const params = req.body
  console.log(params)
  conn.query(
    sql,
    [],
    function(err, result) {
      if (err) {
        console.log(err)
      }
      if (result) {
        jsonWrite(res, result)
      }
    }
  )
})

// 接口：查询全部
router.get('/showStu', (req, res) => {
  const sql = $sql.Stu.show
  const params = req.body
  console.log(params)
  conn.query(
    sql,
    [
      params.mailbox,
      params.name,
      params.role_mg,
      params.state_mg,
      params.phone
    ],
    function(err, result) {
      if (err) {
        console.log(err)
      }
      if (result) {
        jsonWrite(res, result)
      }
    }
  )
})

// 接口：查询
router.post('/showdata1', (req, res) => {
  const sql = $sql.Stu.showdata1
  const params = req.body
  console.log(params)
  conn.query(sql, [params.ID], function(err, result) {
    if (err) {
      console.log(err)
    }
    if (result) {
      jsonWrite(res, result)
    }
  })
})

// 接口：查询
router.post('/showoneStu', (req, res) => {
  const sql = $sql.Stu.showone
  const params = req.body
  console.log(params)
  conn.query(sql, [params.name], function(err, result) {
    if (err) {
      console.log(err)
    }
    if (result) {
      jsonWrite(res, result)
    }
  })
})

router.post('/showoneequipment', (req, res) => {
  const sql = $sql.Stu.showoneequipment
  const params = req.body
  console.log(params)
  conn.query(sql, [params.ID], function(err, result) {
    if (err) {
      console.log(err)
    }
    if (result) {
      jsonWrite(res, result)
    }
  })
})
// 接口：删除信息
router.post('/delStu', (req, res) => {
  const sql = $sql.Stu.del
  const params = req.body
  console.log('删除', params)
  conn.query(sql, [params.name], function(err, result) {
    if (err) {
      console.log(err)
    }
    if (result) {
      jsonWrite(res, result)
    }
  })
})

// 接口：删除信息
router.post('/delequipment', (req, res) => {
  const sql = $sql.Stu.delequipment
  const params = req.body
  console.log('删除', params)
  conn.query(sql, [params.ID], function(err, result) {
    if (err) {
      console.log(err)
    }
    if (result) {
      jsonWrite(res, result)
    }
  })
})

// 接口：修改信息
router.post('/updateStu', (req, res) => {
  const sql = $sql.Stu.update
  const params = req.body
  console.log('修改', params)
  conn.query(
    sql,
    [
      params.mailbox,
      params.name,
      params.role_mg,
      params.state_mg,
      params.phone,
      params.password,
      params.name
    ],
    function(err, result) {
      if (err) {
        console.log(err)
      }
      if (result) {
        jsonWrite(res, result)
      }
    }
  )
})

// 接口：统计数量
router.post('/countshuliang', (req, res) => {
  const sql = $sql.Stu.countshuliang
  const params = req.body
  console.log(params)
  conn.query(sql, [params.sensor], function(err, result) {
    if (err) {
      console.log(err)
    }
    if (result) {
      jsonWrite(res, result)
    }
  })
})

module.exports = router
